/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package models;


import controllers.entity.GiangDay;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author hoangnv
 */
public class GiangDayDAO {
    private static PreparedStatement prst;
    public static List <GiangDay>  getAllGiangDayByMaCanBo(int MaCanBo){
        List <GiangDay> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM GiangDay WHERE MaCanBo = ?");
            prst.setInt(1, MaCanBo);
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maMonHoc = rs.getString("MaMonHoc");
                String tenMonHoc  = rs.getString("TenMon");
                int soTinChi = rs.getInt("SoTinChi");
                String maLopHoc = rs.getString("MaLopHoc");
                int soSinhVien = rs.getInt("SoSinhVien");
                int hocKy = rs.getInt("HocKy");
                int year = rs.getInt("NamHoc");
                        
                GiangDay temp = new GiangDay(maCanBo,maMonHoc,tenMonHoc,soTinChi,maLopHoc,soSinhVien,hocKy,year);        
                
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public static List <GiangDay>  getAllGiangDay(){
        List <GiangDay> list = new ArrayList<>();
        boolean ok = DBConnect.Connect();
        if (!ok) {
            DBConnect.Close();
            return null;
        }
        try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("SELECT * FROM GiangDay");
            ResultSet rs = prst.executeQuery();
            while (rs.next()) {
                int maCanBo = rs.getInt("MaCanBo");
                String maMonHoc = rs.getString("MaMonHoc");
                String tenMonHoc  = rs.getString("TenMon");
                int soTinChi = rs.getInt("SoTinChi");
                String maLopHoc = rs.getString("MaLopHoc");
                int soSinhVien = rs.getInt("SoSinhVien");
                int hocKy = rs.getInt("HocKy");
                int year = rs.getInt("NamHoc");  
                GiangDay temp = new GiangDay(maCanBo,maMonHoc,tenMonHoc,soTinChi,maLopHoc,soSinhVien,hocKy,year);        
                
                list.add(temp);
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
        DBConnect.Close();
        return list;
    }
    public boolean InsertGiangDay(GiangDay X){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("INSERT INTO GiangDay"
                    + "(MaCanBo,MaMonHoc,TenMon,SoTinChi,MaLopHoc,SoSinhVien,HocKy,NamHoc)"
                    + "VALUES (?,?,?,?,?,?,?,?)");
            prst.setInt(1, X.getMaCanBo());
            prst.setString(2,X.getMaMonHoc());
            prst.setString(3,X.getTenMonHoc());
            prst.setInt(4,X.getSoTinChi());
            prst.setString(5,X.getMaLopHoc());
            prst.setInt(6,X.getSoSinhVien());
            prst.setInt(7,X.getHocKy());
            prst.setInt(8,X.getYear());
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean UpdateGiangDay(GiangDay X){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
           prst = (PreparedStatement) DBConnect.con.prepareStatement("UPDATE GiangDay SET "
                   + "MaCanBo = ?,TenMon = ?,SoTinChi = ?,MaLopHoc = ?,SoSinhVien = ?,"
                   + "HocKy = ?,NamHoc = ? WHERE MaMonHoc = ?");    
            prst.setInt(1, X.getMaCanBo());
            prst.setString(2,X.getTenMonHoc());
            prst.setInt(3,X.getSoTinChi());
            prst.setString(4,X.getMaLopHoc());
            prst.setInt(5,X.getSoSinhVien());
            prst.setInt(6,X.getHocKy());
            prst.setInt(7,X.getYear());
            prst.setString(8,X.getMaMonHoc());
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    public boolean DeleteGiangDayByMaLopHoc(String MaLopHoc){
       boolean ok = DBConnect.Connect();
       if (!ok) {
            DBConnect.Close();
            return false;
       }
       try {
            prst = (PreparedStatement) DBConnect.con.prepareStatement("DELETE FROM GiangDay WHERE MaLopHoc = ? ");
            prst.setString(1,MaLopHoc);
            if (prst.executeUpdate() > 0) {
                DBConnect.Close();
                return true;
            }
        } catch (SQLException ex) {
            Logger.getLogger(DBConnect.class.getName()).log(Level.SEVERE, null, ex);
        }
       DBConnect.Close();
       return false;
    }
    
}
